import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiBody,
} from '@nestjs/swagger';
import { DictionaryService } from './dictionary.service';
import type { DictionaryItem } from './dictionary.service';

@ApiTags('字典管理')
@Controller('dictionary')
export class DictionaryController {
  constructor(private readonly dictionaryService: DictionaryService) {}

  @ApiOperation({ summary: '获取所有字典项' })
  @ApiResponse({ status: 200, description: '成功获取字典项' })
  @ApiBearerAuth()
  @Get()
  findAll(): DictionaryItem[] {
    return this.dictionaryService.findAll();
  }

  @ApiOperation({ summary: '根据分类获取字典项' })
  @ApiResponse({ status: 200, description: '成功获取字典项' })
  @Get('category/:category')
  findByCategory(@Param('category') category: string): DictionaryItem[] {
    return this.dictionaryService.findByCategory(category);
  }

  @ApiOperation({ summary: '根据键获取字典项' })
  @ApiResponse({ status: 200, description: '成功获取字典项' })
  @ApiResponse({ status: 404, description: '字典项不存在' })
  @Get('key/:key')
  findByKey(@Param('key') key: string): DictionaryItem {
    const item = this.dictionaryService.findByKey(key);
    if (!item) {
      throw new Error('Dictionary item not found');
    }
    return item;
  }

  @ApiOperation({ summary: '根据ID获取字典项' })
  @ApiResponse({ status: 200, description: '成功获取字典项' })
  @ApiResponse({ status: 404, description: '字典项不存在' })
  @ApiBearerAuth()
  @Get(':id')
  findOne(@Param('id') id: string): DictionaryItem {
    const item = this.dictionaryService.findById(id);
    if (!item) {
      throw new Error('Dictionary item not found');
    }
    return item;
  }

  @ApiOperation({ summary: '创建字典项' })
  @ApiResponse({ status: 201, description: '字典项创建成功' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '字典项数据' })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Body() dictData: Omit<DictionaryItem, 'id' | 'createdAt' | 'updatedAt'>): DictionaryItem {
    return this.dictionaryService.create(dictData);
  }

  @ApiOperation({ summary: '更新字典项' })
  @ApiResponse({ status: 200, description: '字典项更新成功' })
  @ApiResponse({ status: 404, description: '字典项不存在' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '字典项更新数据' })
  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() dictData: Partial<Omit<DictionaryItem, 'id' | 'createdAt'>>,
  ): DictionaryItem {
    const result = this.dictionaryService.update(id, dictData);
    if (!result) {
      throw new Error('Dictionary item not found');
    }
    return result;
  }

  @ApiOperation({ summary: '删除字典项' })
  @ApiResponse({ status: 204, description: '字典项删除成功' })
  @ApiResponse({ status: 404, description: '字典项不存在' })
  @ApiBearerAuth()
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  remove(@Param('id') id: string): void {
    const result = this.dictionaryService.delete(id);
    if (!result) {
      throw new Error('Dictionary item not found');
    }
  }
}